//
//  SetHealthPasswordViewController.m
//  1gene
//
//  Created by 1jiyin on 16/4/6.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "SetHealthPasswordViewController.h"

@interface SetHealthPasswordViewController ()
/*
 *是否设置健康档案密码
 */
@property(nonatomic,assign)BOOL setpassword;
/*
 *健康档案密码
 */
@property(nonatomic,copy)NSString *passeord;
@property(nonatomic,copy)UITextField *text1;

@end

@implementation SetHealthPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)initData
{
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    _setpassword=[user boolForKey:@"sethealthPassword"];
    _passeord=[user objectForKey:@"healthPassword"];
}
-(void)createUI
{
    
    UIView *phoneView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 54, 40)];
    //phoneView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    
    UIView *backgroundView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, phoneView1.frame.size.width-5, phoneView1.frame.size.height)];
    backgroundView1.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    [phoneView1 addSubview:backgroundView1];
    
    
    
    UIImageView *phoneImgView1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 30, 40-2*2-4)];
    UIImage *phoneImg1=[UIImage imageNamed:@"password-3-2"];
    phoneImg1=[phoneImg1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    phoneImgView1.image=phoneImg1;
    [phoneView1 addSubview:phoneImgView1];
    
    
    //密码
    UITextField *oldPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, 20+44+20, self.view.frame.size.width-10*2, 40)];
    oldPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    oldPasswordTextField.leftView=phoneView1;
    oldPasswordTextField.placeholder=@"密码";
    oldPasswordTextField.font=[UIFont systemFontOfSize:15];
    oldPasswordTextField.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    oldPasswordTextField.backgroundColor=[UIColor whiteColor];
    oldPasswordTextField.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    oldPasswordTextField.layer.borderWidth=1.0;
    oldPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:oldPasswordTextField];
    _text1=oldPasswordTextField;
    
    
    //确认修改
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(oldPasswordTextField.frame)+20, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];

    
}
//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    //将密码存到本地
    if (_text1.text.length>0) {
        [self setHealthPassword];
    }else{
        [self.view showTiShiMessage:@"密码不能为空"];
    }
    

}



-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"设置密码";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//是否设置健康档案密码
-(void)isSetHealthPassword
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager GET:[NSString stringWithFormat:IsSetHealthPassword,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //[self.view showTiShiMessage:@"设置成功"];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error%@",error.localizedDescription);
    }];

}

//设置健康档案密码
-(void)setHealthPassword
{
    [self.navigationController.view endEditing:YES];
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    NSDictionary *params=@{@"privacy_pwd":@"",@"newprivacy_pwd":_text1.text,@"newprivacy_pwd_comfired":_text1.text};
    
    [self.view isLoading:YES];
    [manager POST:[NSString stringWithFormat:SetHealthPassword,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"设置健康档案密码%@",dic);
        if ([dic[@"msg"] isEqualToString:@"更新成功"]) {
            NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
            [user setBool:YES forKey:@"sethealthPassword"];
            [user setObject:_text1.text forKey:@"healthPassword"];
            [user synchronize];
            [self.view showTiShiMessage:@"设置成功"];
        }
        NSLog(@"=====%@",dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        NSLog(@"error%@",error);
        [self.view showTiShiMessage:@"已经设置了密码"];
    }];
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
